package cn.lzh.yuanpi;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class yuanPi {
    public static void main(String[] args) {
        JFrame frame = new JFrame("原了个批");
        JPanel panel = new JPanel(null);
        JLabel background = new JLabel(new ImageIcon("images/background.jpg"));
        background.setSize(640,355);
        panel.add(background);
        frame.add(panel);
        frame.setIconImage(new ImageIcon("images/Genshin.png").getImage());
        frame.setSize(640,410);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setVisible(true);
        initGame(panel);
    }
    private static void initGame(JPanel panel) {
        ArrayList<JButton> cards = createCards(16);
        ArrayList<JButton> trough = new ArrayList<>();
        Collections.shuffle(cards);
        poseForCards(cards, panel);
        checkCoverd(cards);
        addAction(cards,trough, panel);
        panel.updateUI();
    }
    private static void addAction(ArrayList<JButton> cards,ArrayList<JButton> trough,JPanel panel) {
        int full = 10;
        for (int i = 0; i < cards.size(); i++) {
            JButton card = cards.get(i);
            card.addActionListener(e->{
                JButton current = (JButton) e.getSource();
            if(trough.size()==full){
                JOptionPane.showMessageDialog(panel,"你真菜，原批！");
                System.exit(0);
            }
            addCardsForTrough(panel,trough,cards,current);
        });
        }
    }
    public static void addCardsForTrough(JPanel panel,ArrayList<JButton> trough,
                                         ArrayList<JButton> cards,JButton current){
        cards.remove(current);
        checkCoverd(cards);
        current.removeActionListener(current.getActionListeners()[0]);
        int index = 0;
        for(;index<trough.size();index++){
            JButton card = trough.get(index);
            if(current.getName().equals(card.getName())){
                break;
            }
        }
        trough.add(index,current);
        removePairedCards(panel,trough,current,index,cards);
        updateTroughCardsLocation(trough,panel);
        panel.updateUI();
    }
    static Timer timer = new Timer();
    private static void removePairedCards(JPanel panel, ArrayList<JButton> trough,
                                          JButton current, int index, ArrayList<JButton> cards) {
        if(trough.size()-index<4){
            return;
        }
        JButton nextNextNextCard = trough.get(index+3);
        if(current.getName().equals(nextNextNextCard.getName())){
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    JButton card = trough.remove(index);
                    panel.remove(card);
                    card = trough.remove(index);
                    panel.remove(card);
                    card = trough.remove(index);
                    panel.remove(card);
                    card = trough.remove(index);
                    panel.remove(card);
                    panel.updateUI();
                    updateTroughCardsLocation(trough,panel);
                    if(cards.isEmpty()){
                        int value = JOptionPane.showConfirmDialog(
                                panel,"再来一局","原批牛的",
                                JOptionPane.YES_NO_OPTION
                        );
                        if(value==JOptionPane.YES_OPTION){
                            initGame(panel);
                        }else
                        {
                            System.exit(0);
                        }
                    }
                }
            },100);
        }
    }
    public static void updateTroughCardsLocation(ArrayList<JButton> trough,JPanel panel){
        for (int i = 0; i < trough.size(); i++) {
            JButton card = trough.get(i);
            card.setLocation(20+i*60,299);
        }
        panel.updateUI();
    }
    private static void checkCoverd(ArrayList<JButton> cards) {
        for (int i = 0; i < cards.size(); i++) {
            JButton c1 = cards.get(i);
            c1.setEnabled(true);
            for (int j = i+1; j < cards.size(); j++) {
                JButton c2 = cards.get(j);
                if(coverd(c1,c2)){
                    c1.setEnabled(false);
                    break;
                }
            }
        }
    }
    private static boolean coverd(JButton c1, JButton c2) {
        int cardOne = c1.getX()+28;
        int cardTwo = c1.getY()+28;
        int cardThree = c2.getX()+28;
        int cardFour = c2.getY()+28;
        double instance = Math.sqrt((cardThree-cardOne)*(cardThree-cardOne)+(cardFour-cardTwo)*(cardFour-cardTwo));
        return instance <56.0;
    }

    public static int random(int num1,int num2){
        Random rand = new Random();
        return rand.nextInt(num2-num1+1)+num1;
    }

    private static void poseForCards(ArrayList<JButton> cards,JPanel panel) {
        for (int i = 0; i < cards.size(); i++) {
            JButton card = cards.get(i);
            card.setLocation(random(20, 620 - 56), random(50, 305 - 56 - 20));
            panel.add(card, 0);
        }
        panel.updateUI();
    }
    public static ArrayList<JButton> createCards(int rounds){
        String[] names = {"Albedo","Amber","Ayaka","Barbara","Bennett","Chongyun","Collei",
                "Cyno","Diluc","Diona","Eula","Fischl","Ganyu","Kaeya","Keqing","Klee","Mona",
                "Ningguang","Noel","Qin","Razor","Sara","Sucrose","Xiangling","Xiao","Xingqiu",
                "Yoimiya"};
        ArrayList<JButton> cards = new ArrayList<>();
        for (int i = 0; i < rounds; i++) {
            for (int j = 0; j < names.length; j++) {
                String name = names[j];
                String filename = "images/"+name+".png";
                String filename2 = "images/"+name+"(1).png";
                JButton card = new JButton(new ImageIcon(filename));
                card.setName(name);
                card.setSize(56,56);
                card.setBorderPainted(false);
                card.setContentAreaFilled(false);
                card.setEnabled(false);
                card.setDisabledIcon(new ImageIcon(filename2));
                cards.add(card);
            }
        }
        return cards;
    }
}
